﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CinemaWS.Model;
using CinemaWS.Model.CustomEntities;

namespace CinemaWS.Repository
{
    public class ShowTimeRepository
    {
        private static CinemaServiceEntities dataEntities = new CinemaServiceEntities();

        private static ShowTimeEntity ConvertToEntity(ShowTime showtime)
        {
            return new ShowTimeEntity
            {
                ID = showtime.ID,
                CinemaFK = CinemaRepository.ConvertToEntity(showtime.Cinema),
                MovieFK = MovieRepository.ConvertToEntity(showtime.Movie),
                Date = showtime.Date.HasValue ? showtime.Date.Value : DateTime.Now,
                Time = showtime.Time
            };
        }

        private static ShowTimeEntity[] ConvertToArray(List<ShowTime> list)
        {
            ShowTimeEntity[] showTimeList = new ShowTimeEntity[list.Count()];
            for (int i = 0; i < list.Count(); i++)
            {
                Add(i, showTimeList, ConvertToEntity(list[i]));
            }
            return showTimeList;
        }

        private static void Add(int index, ShowTimeEntity[] list, ShowTimeEntity showtime)
        {
            list[index++] = showtime;
        }

        public static ShowTimeEntity[] GetAllShowTimeInCurrentDayToNext7Days()
        {
            try
            {
                DateTime next7days = DateTime.Now.AddDays(7);
                var list = (from showtimes in dataEntities.ShowTime
                            where DateTime.Compare(showtimes.Date.Value, DateTime.Now) >= 0
                                 && DateTime.Compare(showtimes.Date.Value, next7days) <= 0
                            select showtimes).ToList();
                return ConvertToArray(list);
            }
            catch
            {
                return null;
            }
        }

        // Maybe need to use?
        public static ShowTimeEntity GetDetail(int showtimeID)
        {
            try
            {
                var showtime = dataEntities.ShowTime.Single(st => st.ID == showtimeID);
                return ConvertToEntity(showtime);
            }
            catch
            {
                return null;
            }
        }

        public static ShowTimeEntity[] GetShowTimesByCinemaAndMovie(int movieID, int cinemaID)
        {
            try
            {
                var list = (from showtimes in dataEntities.ShowTime
                            where showtimes.Movie.ID == movieID
                            select showtimes).ToList();
                return ConvertToArray(list);
            }
            catch
            {
                return null;
            }
        }

        public static ShowTimeEntity[] GetShowTimeByCurrentDate(DateTime date)
        {
            try
            {
                var list = (from showtimes in dataEntities.ShowTime
                            where DateTime.Compare(showtimes.Date.Value, date) == 0
                            select showtimes).ToList();
                return ConvertToArray(list);
            }
            catch
            {
                return null;
            }
        }
        // ====== Maybe need to use?
    }
}